//
//  GEContainerViewController.m
//  GETransformVC
//
//  Created by Grass on 15/8/13.
//  Copyright (c) 2015年 OFashioin. All rights reserved.
//

#import "GEContainerViewController.h"

#import "GEContainerVCHeader.h"

@interface GEContainerViewController ()
<GEContainerProtocol>

@property (nonatomic, assign, readwrite) GEContainerType containerType;

@end

@implementation GEContainerViewController

- (instancetype)init {
    return [self initWithContainerType:kGEContainerTypeNone];
}

- (instancetype)initWithContainerType:(GEContainerType)containderType {
    if (self = [super init]) {
        self.containerType = containderType;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self addChildViewControllerWithContailerType:self.containerType];
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    for (UIViewController *viewCon in self.childViewControllers) {
        viewCon.view.frame = self.view.bounds;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Private
- (void)addChildViewControllerWithContailerType:(GEContainerType)containerType {
    switch (containerType) {
        case kGEContainerTypeNone:
        {
            NSAssert(NO, @"Are you sure yon don't want to load anyone viewController?");
        }
            break;
        case kGEContainerTypeFirst:
        {
            GEFirstViewController *firstVC = [[GEFirstViewController alloc] init];
            firstVC.delegate = self;
            [self addChildViewController:firstVC];
            [self.view addSubview:firstVC.view];
        }
            break;
        case kGEContainerTypeSecond:
        {
            GESecondViewController *secondVC = [[GESecondViewController alloc] init];
            secondVC.delegate = self;
            [self addChildViewController:secondVC];
            [self.view addSubview:secondVC.view];
        }
            break;
        case kGEContainerTypeThird:
        {
            GEThirdViewController *thirdVC = [[GEThirdViewController alloc] init];
            thirdVC.delegate = self;
            [self addChildViewController:thirdVC];
            [self.view addSubview:thirdVC.view];
        }
            break;
    }
}

#pragma mark - GEContainerProtocol
- (void)viewController:(UIViewController *)viewController didSelectedTransformType:(GETransformType)transformType {
    NSAssert([viewController isKindOfClass:[GEContainerBaseViewController class]], @"子类派生错误");
    NSAssert(self.childViewControllers.count == 1, @"过多的child view controller 将会消耗更多的内存");

    [self transformViewControllerToTrandsformType:transformType];
}

#pragma mark - Helper
- (void)transformViewControllerToTrandsformType:(GETransformType)type {
    if (!self.childViewControllers.count) {
        return;
    }
    GEContainerBaseViewController *currentChildviewController = [self.childViewControllers firstObject];
    NSAssert([currentChildviewController isKindOfClass:[GEContainerBaseViewController class]], @"child view controller 派生错误");
    if (currentChildviewController.transformType == type) {
        return;
    }
    
    GEContainerBaseViewController *transViewController = nil;
    switch (type) {
        case kGETransformTypeToNone:
        {
            NSAssert(NO, @"请检查逻辑");
        }
            break;
        case kGETransformTypeToFirst:
        {
            transViewController = [[GEFirstViewController alloc] init];
        }
            break;
        case kGETransformTypeToSecond:
        {
            transViewController = [[GESecondViewController alloc] init];
        }
            break;
        case kGETransformTypeToThird:
        {
            transViewController = [[GEThirdViewController alloc] init];
        }
            break;
        default:
            break;
    }
    if (!transViewController) {
        return;
    }
    transViewController.delegate = self;
    UIViewAnimationOptions option = currentChildviewController.transformType > type ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight;
    [self addChildViewController:transViewController];
    [self transitionFromViewController:currentChildviewController
                      toViewController:transViewController
                              duration:.35f
                               options:option
                            animations:^{
        
    }
                            completion:^(BOOL finished) {
                                if (finished) {
                                    [currentChildviewController removeFromParentViewController];
                                }
    }];
    
}

@end
